package provider.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zhouyongjun
 * @Description:
 * @date 2021/9/2 16:53
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {
    @SentinelResource(value = "provider-get",fallback ="fallback",blockHandler = "flowException")
    @RequestMapping("/get")
    public String get() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",1);
        jsonObject.put("msg","get 成功");
        System.err.println("provoider get 8001 "+new Date());
        return jsonObject.toJSONString();
    }

    @SentinelResource(value = "provider-set",fallback ="fallback",blockHandler = "flowException")
    @RequestMapping("/set")
    public String set() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",1);
        jsonObject.put("msg","set 成功");
        System.err.println("provoider set 8001 "+new Date());
        return jsonObject.toJSONString();
    }

    public String fallback() {
        return "fail back...";
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String flowException(BlockException e) {
        return "限流了！";
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String degradeException(BlockException e) {
        return "降级了！";
    }
}
